package com.mmall.concurrency.example.sync;

import com.mmall.concurrency.annotions.ThreadSafes;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * <p>Descriptions...
 *
 * @author Diamon.Cheng
 * @date 2019/12/4.
 */
@Slf4j
@ThreadSafes
public class Synchronized_1 {
    
    
    // 修饰一个代码块
    public  void    test1(int j){
        synchronized(this){
            for (int  i=0;i<10;i++){
                
                log.info("test1 {} - {}",j,i);
            }
        }
    }
    
    // 修饰一个方法
    public  synchronized  void test2(int j){
        for (int  i=0;i<10;i++){
        
            log.info("test2 {} - {}",j,i);
        }
    
    }
    
    public static void main(String[] args) {
        Synchronized_1 example1 = new Synchronized_1();
        Synchronized_1 example2 = new Synchronized_1();
        ExecutorService executorService = Executors.newCachedThreadPool();
        executorService.execute(() -> {
            example1.test2(1);
        });
        executorService.execute(() -> {
            example2.test2(2);
        });
    }
    

}
